// home/enterprise/SearchEnterprise.js
import api from "../../utils/api.js";
import network from "../../utils/network.js";
const app = getApp();
let pageIndex = 0;
let nowTotal = 0;
let total = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    category: [{
      id: 0,
      name: '全部'
    }],
    nature: [{
      id: 0,
      name: '全部'
    }],
    // region: ['全国', '', '', '', '', '', '', ''],
    categoryOpen: false,
    natureOpen: false,
    categoryIndex: 0,
    natureIndex: 0,
    searchtext:''
    // regionOpen: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let searchtext = options.searchtext;
    this.setData({
      searchtext: searchtext,
    })
    this.onPullDownRefresh();
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    pageIndex = 0;
    nowTotal = 0;
    total = 0;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    pageIndex = 0;
    this.setData({
      list: [],
    })
    this.request();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.request();
  },

  /**
   * 请求数据
   * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
   */
  request: function() {
    if (pageIndex > 0 && total <= nowTotal) {
      this.toast.showToast("暂无更多");
    } else {
      network.postJson(this, api.searchQiye, pageIndex, {
        searchtext: this.data.searchtext
      }).then(res => {
        this.setData({
          [`list[${pageIndex}]`]: res.data.data.rows,
          count: res.data.data.count
        })

        console.log(this.data.list)

        if (pageIndex == 1) {
          nowTotal = this.data.list[0].length;
        } else if (pageIndex > 1) {
          nowTotal = (pageIndex - 1) * 20 + this.data.list[pageIndex].length;
        }
        pageIndex++;
        total = res.data.data.count;
      });
    }
  },

  categoryTap() {
    this.setData({
      categoryOpen: !this.data.categoryOpen,
      natureOpen: false,
      // regionOpen: false
    })
  },

  natureTap() {
    this.setData({
      categoryOpen: false,
      natureOpen: !this.data.natureOpen,
      // regionOpen: false
    })
  },

  // regionTap() {
  //   this.setData({
  //     categoryOpen: false,
  //     natureOpen: false,
  //     regionOpen: !this.data.regionOpen
  //   })
  // },

  /**
   * 点击分类
   */
  onClassItemClick: function(e) {
    let index = e.currentTarget.dataset.index;    
    this.setData({
      categoryOpen: false,
      categoryIndex: index
    })
    this.onPullDownRefresh();
  },

  /**
   * 点击企业性质
   */
  onNatureItemClick: function(e) {
    let index = e.currentTarget.dataset.index;   
    this.setData({
      natureOpen: false,
      natureIndex: index
    })
    this.onPullDownRefresh();
  },

  goEnterprise: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/home/enterprise/Enterprise?id=' + id
    })
  },

  goProductDetails: function() {
    wx.navigateTo({
      url: '/home/enterprise/ProductDetails'
    })
  },
  searchinput: function(e) {
    this.setData({
      searchtext: e.detail.value
    })
  },
  seclaer:function(e){
    this.setData({
      searchtext: ''
    })
  },
   //搜索
   search: function(e) {
    var searchtext = this.data.searchtext; //搜索框的值
    if (searchtext != "") {
      wx.stopPullDownRefresh();
      pageIndex = 0;
      this.setData({
        list: [],
      })
      this.request()
    } else {
      return;
    }
  },
})